In [9]:
import numpy as np
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import axes3d
In [39]:
#Define constants - speed of light, frequency, electric field amplitude, wave number.
c = 2.99792 *10**8
w = 4
E_0 = 2
k = 1
In [40]:
#Define a function for the electric field, which only depends on x in this case.
def E(x, t):
return E_0 * np.cos(-k*x - w*t)
In [41]:
#Define a function for the magnetic field, which only depends on x in this case.
def B(x, t):
return (1/c * E_0) * np.cos(-k*x - w*t)
In [59]:
#Although this is a plane wave, plot the wave along a single line along the vector k = (-1, 0, 0).
fig = plt.figure(figsize = (10, 10))
ax = fig.gca(projection = '3d')
#Choose a time (in seconds) to plot the electric and magnetic fields.
t = 0
#Make a grid of points where vectors of the vector field are placed.
x_lim = 10
X = np.arange(-x_lim, x_lim, 0.5)
Y = 0
Z = 0
U_electric = 0
V_electric = 0
W_electric = E(X, t)
U_magnetic = 0
V_magnetic = B(X, t)
W_magnetic = 0
#Plot the vector field - electric field.
ax.quiver(X, Y, Z, U_electric, V_electric, W_electric, color = 'orange', arrow_length_ratio = 0.03, linewidths = 1, label = 'Electric Field')
#Plot the vector field - magnetic field.
ax.quiver(X, Y, Z, U_magnetic, V_magnetic, W_magnetic, color = 'blue', arrow_length_ratio = 0.03, linewidths = 1, label = 'Magnetic Field')
#Adjust plot size.
ax.set_xlim3d(-10, 10)
ax.set_ylim3d(-10, 10)
ax.set_zlim3d(-10, 10)
#Label the plot.
ax.set_xlabel('x (meters)')
ax.set_ylabel('y (meters)')
ax.set_zlabel('z (meters)')
ax.set_title('Electric and Magnetic Fields of Electromagnetic Wave \n Traveling Along X-Axis', fontsize = 16)
ax.legend(loc = 'lower left')
plt.show()
In [60]:
#Although this is a plane wave, plot along a single line along the vector k = (-1, 0, 0).
fig = plt.figure(figsize = (10, 10))
ax = fig.gca(projection = '3d')
#Choose a time (in seconds) to plot the electric and magnetic fields.
t = 0
#Make a grid of points where vectors of the vector field are placed.
x_lim = 10
X = np.arange(-x_lim, x_lim, 0.5)
Y = 0
Z = 0
U_electric = 0
V_electric = 0
W_electric = E(X, t)
U_magnetic = 0
V_magnetic = B(X, t)
W_magnetic = 0
#Plot the vector field - electric field.
ax.quiver(X, Y, Z, U_electric, V_electric, W_electric, color = 'orange', arrow_length_ratio = 0.03, linewidths = 1, label = 'Electric Field')
#Plot the vector field - magnetic field.
ax.quiver(X, Y, Z, U_magnetic, V_magnetic, W_magnetic, color = 'blue', arrow_length_ratio = 0.03, linewidths = 1, label = 'Magnetic Field')
#Adjust plot size.
ax.set_xlim3d(-10, 10)
ax.set_ylim3d(-10/c, 10/c)
ax.set_zlim3d(-10, 10)
#Adjust the viewing angle of the plot.
ax.view_init(elev = 20, azim = 170)
#Label the plot.
ax.set_xlabel('x (meters)')
ax.set_ylabel('y (meters)')
ax.set_zlabel('z (meters)')
ax.set_title('Electric and Magnetic Fields of Electromagnetic Wave \n Traveling Along X-Axis', fontsize = 16)
ax.legend(loc = 'lower left')
plt.show()